<?php
namespace Manage\Controller;
use Common\Controller\ManageBaseController;
use Think\Model;
use Common\Model\ArticleModel;



class ArticleController extends ManageBaseController{
	
	public function lists($ajax=null,$status=null,$page=null,$rows=null,$type = NULL) {
		$map = array(); $status_view = array('default'=>'所有','del'=>'已删除','forbid'=>'禁用','allow'=>'正常');
		//查询条件 处理
		$now_status = $status_view['default'];
		if (isset($status) && key_exists($status, ArticleModel::$mystat)) { //指定查询状态
			$map['status'] = ArticleModel::$mystat[$status];
			$now_status = $status_view[$status];
		}else {
			$map['status'] = array('EGT',0); //默认查询状态为未删除的数据
		}
		if ($page <= 0) {
			$page = 1;
		}
		if ($rows > 0) {
			$listRows = $rows;
		}else {
			$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
		}
		
		//筛选条件
		$filter = I('param.');
		if(!empty($filter['cid'])){ //文章所属分类
			$map['cate']=$filter['cid'];
			$this->assign('cate', $map['cate']);
		}
		if ($filter['keyword']) {
			$map['title'] = array('like','%'.$filter['keyword'].'%');
			$this->assign('keyword', $filter['keyword']);
		}
		/******************/
		if ($ajax==='y') {
			$model = new Model('article');
			$total = $model->where($map)->count(); //总记录数
			$list = $model->where($map)->order('id desc')->page("$page,$listRows")->select();
			
			//list数据处理
			foreach ($list as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['updatetime']); 
			}
			unset($v);		
// 			print_r($list);
			$list = array("total"=>(int)$total,"rows"=>$list);
			$this->ajaxReturn($list);
		}else {
			$this->assign('_page', $page);
			$this->assign('_rows', $listRows);
			$this->assign('now_status',$now_status); //当前页面筛选的状态
			
			// 记录当前列表页的cookie
			cookie(C('CURRENT_URL_NAME'),$_SERVER['REQUEST_URI']);
			if($type){
				$this->display($type);
			}else{
				$this->display();
			}		
		}
	}
	/*插入一条记录*/
	public function insert($title,$cate,$content = NULL) {
		$return = array('status'=>0,'info'=>'');
		
		$model = new ArticleModel();

		$data = array('content'=>$content,'title'=>$title,'cate'=>$cate,'updatetime'=>NOW_TIME);
		
		$data['picurl'] = $_FILES['thumb'];
		if (empty($_FILES['thumb']) || $data['picurl']['error']=='4') {
			unset($data['picurl']);
		}
		if (false === $model->myAdd($data)) {
			$return['info'] = $model->getError();
		}else {
			$return['status'] = 1;
			$return['info'] = '添加成功';
		}
		
		$this->ajaxReturn($return);
	}
	/*删除文章记录*/
	public function delete($id) {
		$return = array('status'=>0,'info'=>'');
		$model = new ArticleModel();
		$model->where('id='.$id)->delete();
		$return['status']=1;
		$return['info']='删除成功';
		$this->ajaxReturn($return);
	}
	/**
	 * 文章详情页面
	 * @param 文章ID $id
	 */
	public function info($id) {
		$model = new Model('Article');
		$info = $model->find($id);
		$this->assign('info', $info);
		$this->display();
		
	}
	
	/**
	 * ajax更新
	 * @param unknown $id
	 * @param unknown $content
	 */
// 	public function update($id,$content,$title=null) {
// 		$return = array('status'=>0,'info'=>'');
		
// 		$model = new Model('Article');
// 		$data = array('content'=>$content,'updatetime'=>NOW_TIME);
// 		if (!empty($title)) {
// 			$data['title'] = $title;
// 		}
		
		
// 		if ( false === $model->where('`id`='.$id)->save($data) ) {
// 			$return['info'] = '保存失败';
// 		}else {
// 			$return['status'] = 1;
// 			$return['info'] = '保存成功';
// 		}
		
// 		$this->ajaxReturn($return);
// 	}
	public function update() {
		$return = array('status'=>0,'info'=>'');
		$data = I('post.');
		$data['content'] = $_POST['content'];
		$data['updatetime'] = time();
		if ($data['id']<=0) {
			$return['info']='请先选择要更新的记录';
			$this->ajaxReturn($return);
		}
	
		$model = new ArticleModel();
		//门店
		$data['picurl'] = $_FILES['thumb'];
		if (empty($_FILES['thumb']) || $data['picurl']['error']=='4') {
			unset($data['picurl']);
		}
	
		if (false === $model->myUpdate($data)) {
			$return['info'] = $model->getError();
		}else {
			$return['status'] = 1;
			$return['info'] = '更新成功';
		}
		$this->ajaxReturn($return);
	}
	
	/**
	 * notice 0,1 切换
	 */
	public function notice($id) {
		$id = (int)$id;
	
		$model = new Model('Article');
		$info = $model->where("`id`=".$id)->field('id,notice')->find();
		$newstate =  ($info['notice'] > 0) ? '0' : '1';
		if (false === $model->where('`id`='.$id)->setField('notice',$newstate)) {
			$this->error('更新失败,未知错误!');
		}
		$this->success('更新成功');
	}
	
	
	/**
	 * status 0,1 切换
	 */
	public function state($id) {
		$id = (int)$id;
	
		$model = new Model('Article');
		$info = $model->where("`id`=".$id)->field('id,status')->find();
		$newstate =  ($info['status'] > 0) ? '0' : '1';
		if (false === $model->where('`id`='.$id)->setField('status',$newstate)) {
			$this->error('更新失败,未知错误!');
		}
		$this->success('更新成功');
	}
}